package com.jeect.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeect.base.util.ResponseBody;
import com.jeect.base.util.ResponseCode;
import com.jeect.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@RestController
public class AuthenticationController {
    private Logger logger = LoggerFactory.getLogger(AuthenticationController.class);
    private RequestCache requestCache = new HttpSessionRequestCache();
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    ObjectMapper objectMapper;

    @RequestMapping("/user/userLogin")
    @ResponseStatus(code = HttpStatus.UNAUTHORIZED)
    public ResponseBody userLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SavedRequest savedRequest = requestCache.getRequest(request, response);
        RedisUtil redisUtil = new RedisUtil(redisTemplate);

        Cookie[] cookies = request.getCookies();

        Enumeration<String> headers = request.getHeaderNames();
        while (headers.hasMoreElements()) {
            String name = headers.nextElement();
            System.out.println(name + ":" + request.getHeader(name));
        }
//        RequestContext context = RequestContext.getCurrentContext();
//        context.addZuulRequestHeader("Cookie","SESSION="+sessionid);
        String sessionid = request.getSession().getId();
        System.out.println("AuthenticationController.............userLogin().");
        System.out.println("sessionid===="+sessionid);
        System.out.println("redis session = "+redisUtil.get(sessionid));
        if (savedRequest != null) {
            String url = savedRequest.getRedirectUrl();
            logger.info("您跳转的URL地址为:" + url);
            Authentication authentication = (Authentication) request.getSession().getAttribute("authentication");
            if (StringUtils.endsWithIgnoreCase(url, ".html")|| (authentication!=null&& authentication.getName()!=null) ) {
                redirectStrategy.sendRedirect(request, response, url);
                return null;
   //         return new ResponseBody(ResponseCode.FAILE, "未登录...");
            }
        }
        return new ResponseBody(ResponseCode.FAILE, "未授权");
    }
}
